package com.xiaoxin.exception;

import com.xiaoxin.model.vo.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author xiaoxiaode
 * @date 2021-08-30-17:23
 **/
/*
 * 自定义统一异常处理
 * 以注解声明异常处理类
 * 编写方法,在方法内部处理数据,构造响应数据
 * 方法上编写注解,声明此方法可以处理的异常类型
 * */
@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handleException(BusinessException be) {
        ErrorResult errorResult = be.getErrorResult();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity handleException(Exception be) {
        be.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }

}
